﻿using FineUI;
using SysManage.BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;
using ZHDJ.Data.Models.Grassroots;
using ZHDJ.WebPage;

namespace SysManage.Module.Grassroots
{
    public partial class GroupReportList : PageBase
    {
        #region 属性
        public override string ViewPower
        {
            get
            {
                return "GroupReportView";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public string IsBranch
        {
            get
            {
                return this.Request["IsBranch"] ?? "";

            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        #region 数据加载
        protected void BindData()
        {
            this.ppBranch.Value = this.BranchID;
            this.ppBranch.Text = ControlBusiness.GetBranchPickerText(this.BranchID);

            ControlBusiness.ControlBind(ddlGroup, PartyGrassrootsGroup.GetGroupData("", IsSystemAdmin ? "" : this.BranchID), "GroupName", "ID", true);
        }

        private void BindGrid()
        {
            var ds = PartyGrassrootsGroupItem.GetReportData(Grid1.PageIndex, Grid1.PageSize, ddlGroup.SelectedValue, ddlItem.SelectedValue, dpBeginDate.Text, dpEndDate.Text, ppBranch.Value);

            Grid1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            Grid1.DataSource = ds.Tables[0];
            Grid1.DataBind();
        }

        private void LoadData()
        {
            // 权限检查
            CheckPowerWithButton("GroupReportAdd", btnAdd);

            btnAdd.OnClientClick = Dialog.GetShowReference("GroupReportEdit.aspx?", "新增数据");

            // 每页记录数
            Grid1.PageSize = ConfigHelper.PageSize;

            BindGrid();
        }

        protected void Grid1_PreDataBound(object sender, EventArgs e)
        {
            // 数据绑定之前，进行权限检查
            CheckPowerWithWindowField("GroupReportEdit", Grid1, "editField");
            CheckPowerWithLinkButtonField("GroupReportDelete", Grid1, "deleteField");
        }

        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            BindGrid();
        }

        protected void ddlGridPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlGridPageSize.SelectedValue);
            BindGrid();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            string id = GetSelectedDataKeyID(Grid1);

            if (e.CommandName == "Delete")
            {
                // 在操作之前进行权限检查
                if (!CheckPower("GroupReportDelete"))
                {
                    CheckPowerFailWithAlert();
                    return;
                }

                var userCount = DB.PartyGrassrootsGroupItems.Count(t => t.ID == id);
                if (userCount > 0)
                {
                    Alert.ShowInTop("删除失败，已发布的数据不能删除！");
                    return;
                }

                var del = DB.PartyGrassrootsGroupItems.FirstOrDefault(t => t.ID == id);
                DB.PartyGrassrootsGroupItems.Remove(del);
                DB.SaveChanges();
                BindGrid();
            }
        }
        #endregion

        #region 按钮事件
        protected void ppBranch_TextChanged(object sender, EventArgs e)
        {
            ppBranch.ShowTrigger1 = !string.IsNullOrEmpty(ppBranch.Text);
        }

        protected void btnDeleteSelected_Click(object sender, EventArgs e)
        {
            // 在操作之前进行权限检查
            if (!CheckPower("GroupReportDelete"))
            {
                CheckPowerFailWithAlert();
                return;
            }

            // 从每个选中的行中获取ID（在Grid1中定义的DataKeyNames）
            List<string> ids = GetSelectedDataKeyIDs(Grid1);

            // 执行数据库操作
            DB.PartyGrassrootsGroupItems.Where(u => ids.Contains(u.ID)).ToList().ForEach(u => DB.PartyGrassrootsGroupItems.Remove(u));
            DB.SaveChanges();

            // 重新绑定表格
            BindGrid();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void ddlGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            ControlBusiness.ControlBind(ddlItem, PartyGrassrootsGroupItem.QueryItemDataByGroupID(ddlGroup.SelectedValue, IsSystemAdmin ? "" : this.BranchID), "ItemName", "ID", true);
        }
        #endregion

        protected void Dialog_Close(object sender, EventArgs e)
        {
            BindGrid();
        }
    }
}